import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;

import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;


public class DataSimulator {

	private static DateTime start = new DateTime(2013, 4, 1, 0, 0, 0, 0);
	private static final DateTime end = new DateTime(2013, 4, 30, 0, 0, 0, 0);

	private final static Period sleep_1 = new Period(8, 0, 0, 0); // Room 1 bedroom
	private final static Period bath_1 = new Period(1, 0, 0, 0); // Room 2 bathroom
	private final static Period breakfast = new Period(0, 30, 0, 0); // Room 3 dining
	private final static Period wandering_1 = new Period(2, 30, 0, 0);// wandering
	private final static Period lunch = new Period(1, 0, 0, 0); // Room 3 dining
	private final static Period sleep_2 = new Period(2, 0, 0, 0); // Room 1 bedroom
	private final static Period tea = new Period(0, 30, 0, 0); // room 3 dining
	private final static Period wandering_2 = new Period(2, 30, 0, 0); // wandering
	private final static Period dinner = new Period(1, 0, 0, 0); // room 3 dining
	private final static Period bath_2 = new Period(1, 0, 0, 0); // room 2 bathroom
	private final static Period sleep_3 = new Period(4, 0, 0, 0); // room 1 bedroom

	private final static Period day = new Period(24 * 3600);

	public static ArrayList<Period> activities = new ArrayList<>();

	@SuppressWarnings("unused")
	private int activityLevel = 0;


	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {

		addActivities();
		
		int x = 1;

		for(Period p : activities)
		{
			while(p.toStandardSeconds().getSeconds() > 0)
			{
				int interV = getInterval();
				int pSec = p.toStandardSeconds().getSeconds();
				
				while(start.isBefore(end))
				{
					p = p.minusSeconds(interV);
					start = start.plusSeconds(interV);

					DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
					String formattedDate = start.toString( fmt );

					String s = getRoomNumber(x) + ", " + getTemperature() + ", \"" + formattedDate + "\", " + getActivityLevel(start);
					writeToArff(s);
					System.out.println(s);
				}
			}
			
			x++;
		}

	}

	private static int getInterval() {
		
		int result = 18 + (int)(Math.random() * ((95 - 18) + 1));
		
		return result;
	}

	private static void addActivities() {

		activities.clear();

		activities.add(sleep_1);
		activities.add(bath_1);
		activities.add(breakfast);
		activities.add(wandering_1);
		activities.add(lunch);
		activities.add(sleep_2);
		activities.add(tea);
		activities.add(wandering_2);
		activities.add(dinner);
		activities.add(bath_2);
		activities.add(sleep_3);

	}

	private static int getTemperature()
	{
		int result = 18 + (int)(Math.random() * ((23 - 18) + 1));

		return result;
	}

	private static int getRoomNumber(int period)
	{
		int result = 0, min, max;
		
		switch(period)
		{
		case 1:
			result = 1;
			break;
		case 2:
			result = 2;
			break;
		case 3:
			result = 3;
			break;
		case 4:
			min = 1;
			max = 4;
			result = min + (int)(Math.random() * ((max - min) + 1));
			break;
		case 5:
			result = 3; 
			break;
		case 6:
			result = 1;
			break;
		case 7:
			result = 3;
			break;
		case 8:
			min = 1;
			max = 6;
			result = min + (int)(Math.random() * ((max - min) + 1));
			break;
		case 9:
			result = 3; 
			break;
		case 10:
			result = 2; 
			break;
		case 11:
			result = 1; 
			break;
		}
		
		//int result = 1 + (int)(Math.random() * ((6 - 1) + 1));

		return result;
	}

	private static int getActivityLevel(DateTime start2)
	{
		final int maximumActivityLevel = 14500;

		int currentPeriod = start2.minuteOfDay().get()*60;
		int dayPeriod = day.toStandardSeconds().getSeconds();

		return maximumActivityLevel * (currentPeriod/dayPeriod);
	}

	private static void writeToArff(String s) throws Exception
	{
		final String fileLoc = "C:\\Users\\colin\\Documents\\Magic Briefcase\\MSc Documents\\Research Thesis\\Programming\\datagen.arff";
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileLoc, true)));
		out.print("\n" + s);
		out.close();
	}

}
